﻿using System.Collections.Generic;
using System.Linq;
using studentSubsidy.Models;
using studentSubsidy.Utils;

namespace studentSubsidy.Dao
{
    public class AdminSignedDao
    {
        private MyDbContext DbContext = new MyDbContext();

        public List<Apply> getSignedList(string role)
        {
            if (role == "school")
            {
                return DbContext.Applies.Where(p => p.isCheck == 1 && p.isCityCheck == -1 && p.isSigned == 0).ToList();
            }
            if (role == "bureau")
            {
                return DbContext.Applies.Where(p => p.isCheck == 1 && p.isCityCheck == 1 && p.isSigned == 0).ToList();
            }
            return null;
        }
        
        public List<Apply> getHasSignedList(string role)
        {
            if (role == "school")
            {
                return DbContext.Applies.Where(p => p.isCheck == 1 && p.isCityCheck == -1 && p.isSigned == 1).ToList();
            }
            if (role == "bureau")
            {
                return DbContext.Applies.Where(p => p.isCheck == 1 && p.isCityCheck == 1 && p.isSigned == 1).ToList();
            }
            return null;
        }

        public List<Signed_File> getSignedFileById(int applyId)
        {
            return DbContext.SignedFiles.Where(p => p.apply_id == applyId).ToList();
        }

        public void deleteSignedFile(int applyId, int type)
        {
            Signed_File signedFile = DbContext.SignedFiles.Where(p => p.apply_id == applyId && p.fileType == type).ToList().First();
            DbContext.SignedFiles.Remove(signedFile);
            DbContext.SaveChanges();
        }

        public void rejectSigned(int applyId)
        {
            Signed_File signedFile = DbContext.SignedFiles.Where(p => p.apply_id == applyId && p.fileType == 2).ToList()
                .First();
            signedFile.isCheck = -1;
            DbContext.SignedFiles.Update(signedFile);
            DbContext.SaveChanges();
        }

        public Signed_File getUserSignedFile(int applyId)
        {
            return DbContext.SignedFiles.Where(p => p.apply_id == applyId && p.fileType == 2).ToList().First();
        }

        public void signedFileSuccess(int applyId)
        {
            Signed_File signedFile = DbContext.SignedFiles.Where(p => p.apply_id == applyId && p.fileType == 2).ToList().First();
            Apply apply = DbContext.Applies.Where(p => p.apply_id == applyId).ToList().First();
            apply.isSigned = 1;
            signedFile.isCheck = 1;
            DbContext.SignedFiles.Update(signedFile);
            DbContext.Applies.Update(apply);
            DbContext.SaveChanges();
        }

        public List<Apply> getSignedNeedCheckList(string role)
        {
            if (role == "school")
            {
                return DbContext.Applies.Where(p => p.isSigned == 2 && p.isCityCheck == -1).ToList();
            }
            if (role == "bureau")
            {
                return DbContext.Applies.Where(p => p.isSigned == 2 && p.isCityCheck == 1).ToList();
            }

            return null;
        }
        
    }
}